import {
  GetAuthInfo,
  getLink,
  GetAuthStatus,
  GetPreAuthUrl,
  GoToWeixin,
  GetAuthStoreInfo,
  addXCXAppid,
  GetAppIdList,
  delXCXAppid,
  GetTesterList,
  BindTester,
  UnBindTester,
  GetDomainInfo,
  ResetDomain,
  GetAuthException,
  GetWxaCode
} from '@/services/xcxauth';
import {catchException} from '@/utils/utils';

export default {
  namespace: 'xcxauth',

  state: {
    authInfo: {},
    authStoreInfo: {},
    authLink: '',
    menuData: [],
    principal:{},
    errorInfo:{},
  },
  effects: {
    *GetAuthInfo({ payload, callback }, { call, put }) {
      const data = yield call(GetAuthInfo);
      yield catchException(function* (){
        if (data ) {
            let selected = data.Data.Select.split(',');
            let Permission = data.Data.Permission;
            selected.forEach(item => {
              Permission.forEach(v => {
                if (item == v.Id) {
                  v.Auth = true;
                }
              });
            });
            yield put({
              type: 'saveGetAuthInfo',
              payload: data.Data,
            });
            callback && callback.success && callback.success(data);
          }
      })
    },
    *GetAuthException({ payload, callback }, { call, put }) {
      const data = yield call(GetAuthException);
      yield catchException(function* (){
        if (data) {
            yield put({
              type: 'saveGetAuthException',
              payload: data.Data,
            });
            callback && callback.success && callback.success(data);
          }
      })
    },
    *getLink(_, { call, put }) {
      const data = yield call(getLink);
      yield catchException(function* (){
        if (data) {
            yield put({
              type: 'savegetLink',
              payload: data.Data,
            });
          }
      })
    },
    *GetAuthStatus({ payload, callback }, { call, put }) {
      const data = yield call(GetAuthStatus);
      yield catchException(function* (){
        if (data) {
            yield put({
              type: 'saveGetAuthStatus',
              payload: data.Data,
            });
            callback && callback.success && callback.success(data);
          }
      })
    },
    *GetPreAuthUrl({ payload, callback }, { call, put }) {
      const data = yield call(GetPreAuthUrl);
      yield catchException(function* (){
        if (data) {
            yield put({
              type: 'saveGetPreAuthUrl',
              payload: data.Data,
            });
            callback && callback.success && callback.success(data);
          }
      })
    },
    *GoToWeixin({ payload, callback }, { call, put }) {
      const go = () => GoToWeixin(payload);
      const data = yield call(go);
      yield catchException(function* (){
        if (data) {
            yield put({
              type: 'saveGoToWeixin',
              payload: data.Data,
            });
            callback && callback.success && callback.success(data);
          }
      })
    },
    *GetAuthStoreInfo({ payload, callback }, { call, put }) {
      const data = yield call(GetAuthStoreInfo);
      yield catchException(function* (){
        if (data) {
            yield put({
              type: 'saveGetAuthStoreInfo',
              payload: data.Data,
            });
            callback && callback.success && callback.success(data);
          }
      })
    },
    *addXCXAppid({ payload, callback }, { call, put }) {
      const data = yield call(addXCXAppid,payload);
      yield catchException(function* (){
        if (data) {
            yield put({
              type: 'saveAddXCXAppid',
              payload: data.Data,
            });
            callback && callback.success && callback.success(data);
          }
      })
    },
    *GetAppIdList({ payload, callback }, { call, put }) {
      const data = yield call(GetAppIdList);
      yield catchException(function* (){
        if (data) {
            yield put({
              type: 'saveAppIdList',
              payload: data.Data,
            });
            callback && callback.success && callback.success(data);
          }
      })
    },
    *delXCXAppid({ payload, callback }, { call, put }) {
      const data = yield call(delXCXAppid,payload);
      yield catchException(function* (){
        if (data) {
            yield put({
              type: 'saveDelXCXAppid',
              payload: data.Data,
            });
            callback && callback.success && callback.success(data);
          }
      })
    },
    *GetTesterList({ payload, callback }, { call, put }) {
      const data = yield call(GetTesterList,payload);
      yield catchException(function* (){
        if (data) {
            yield put({
              type: 'saveTesterList',
              payload: data.Data,
            });
            callback && callback.success && callback.success(data);
          }
      })
    },
    *BindTester({ payload, callback }, { call, put }) {
      const data = yield call(BindTester,payload);
      yield catchException(function* (){
        if (data) {
            yield put({
              type: 'saveBindTester',
              payload: data.Data,
            });
            callback && callback.success && callback.success(data);
          }
      })
    },
    *UnBindTester({ payload, callback }, { call, put }) {
      const data = yield call(UnBindTester,payload);
      yield catchException(function* (){
        if (data) {
            yield put({
              type: 'saveUnBindTester',
              payload: data.Data,
            });
            callback && callback.success && callback.success(data);
          }
      })
    },
    *GetDomainInfo({ payload, callback }, { call, put }) {
      const data = yield call(GetDomainInfo);
      yield catchException(function* (){
        if (data) {
            yield put({
              type: 'saveDomainInfo',
              payload: data.Data,
            });
            callback && callback.success && callback.success(data);
          }
      })
    },
    *ResetDomain({ payload, callback }, { call, put }) {
      const data = yield call(ResetDomain,payload);
      yield catchException(function* (){
        if (data&&data.Code === 0) {
            yield put({
              type: 'saveResetDomain',
              payload: data.Data,
            });
            callback && callback.success && callback.success(data);
          }else {
            callback && callback.error(data)
          }
      })
    },
    *GetWxaCode({ payload, callback }, { call, put }) {
      const data = yield call(GetWxaCode);
      yield catchException(function* (){
        if (data&&data.Code === 0) {
            yield put({
              type: 'saveGetWxaCode',
              payload: data.Data,
            });
            callback && callback.success && callback.success(data);
          }
      })
    },
  },

  reducers: {
    saveGetAuthInfo(state, { payload }) {
      //console.log(payload, 456);
      return {
        ...state,
        authInfo: payload,
      };
    },
    saveGetAuthStoreInfo(state, { payload }) {
      return {
        ...state,
        authStoreInfo: payload,
      };
    },
    savegetLink(state, { payload }) {
      return {
        ...state,
        authLink: payload,
      };
    },
    saveGetAuthStatus(state, { payload }) {
      //console.log(payload)
      return {
          ...state,
          authStatus: payload,
      };
    },
    saveAppIdList(state, { payload }) {
      return {
          ...state,
          appIdList: payload,
      };
    },
    saveTesterList(state, { payload }) {
      return {
          ...state,
          testerList: payload,
      };
    },
    saveDomainInfo(state, { payload }) {
      return {
          ...state,
          domainInfo: payload,
      };
    },
    saveGetAuthException(state, { payload }) {
      return {
          ...state,
          errorInfo: payload,
      };
    },
  },
};
